package com.briup.common.sms;

import com.alibaba.fastjson.JSON;
import com.zhenzi.sms.ZhenziSmsClient;

import java.util.HashMap;
import java.util.Map;

/**
 * 短信验证码工具类-榛子云短信
 */
public class ZhenZiYunSmsUtil {

    // 榛子云api
    private final static String apiUrl = "https://sms_developer.zhenzikj.com";

    // appId
    private final static String appId = "108182";

    // 密钥
    private final static String appSecret = "16fee747-ac63-49f3-900f-xxxxxxxxx";

    // 短信模板ID
    public static final String TEMPLATE_ID = "3774";


    /**
     * 向某个手机号发送验证码
     * @param telephone 手机号
     * @param map 参数
     */
    public static boolean sendSms(String telephone, Map<String,String> map) {
        try {
            ZhenziSmsClient client = new ZhenziSmsClient(apiUrl, appId, appSecret);
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("number", telephone);
            params.put("templateId", TEMPLATE_ID);

            String[] templateParams = new String[2];
            templateParams[0] = map.get("code");
//            templateParams[1] = String.valueOf(TIME / 60);
            templateParams[1] = map.get("time");
            params.put("templateParams", templateParams);

            String result = client.send(params);
            System.out.println(JSON.parseObject(result).get("code").getClass());

        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }
}
